import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import { fileURLToPath, URL } from 'node:url'
import postcsspxtoviewport from 'postcss-px-to-viewport'
const vw = postcsspxtoviewport({
  // 视口宽度，一般就是 375（ 设计稿一般采用二倍稿，宽度为 375 ）
  viewportWidth: 375
})
// https://vitejs.dev/config/
const pathSrc = fileURLToPath(new URL('./src', import.meta.url))
export default defineConfig(({ command, mode }) => {
  return {
    plugins: [react()],
    resolve: {
      alias: {
        '@': pathSrc
      }
    },
    css: {
      postcss: {
        plugins: [vw]
      }
    },
    server: {
      proxy: {
        '/api': {
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
    clearScreen: false, //避免清屏
    build: {
      // sourcemap: true //打包添加sourcmap文件，便于打包分析
      // rollupOptions: {
      //   external(source, importer, isResolved) {
      //     console.log(source, importer, isResolved)
      //   }
      // }
    }
  }
})
